﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProjectWebNhac.BUS;
using System.Data;
using ProjectWebNhac.DTO;

namespace ProjectWebNhac.GUI.User
{
    public partial class ChiTietAlbum : System.Web.UI.Page
    {
        int MaAlbum ;
        int MaBXH =5;
        BaiHatBus baihatBus = new BaiHatBus();
        AlbumBus abbus = new AlbumBus();
        CaSiBus csbus = new CaSiBus();
        BXHBus bxhbus = new BXHBus();
        BinhLuanAlbumBus blAlbum = new BinhLuanAlbumBus();
        int MaTheLoai;
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!Page.IsPostBack)
            {
                if (Request["MaAlbum"] != null)
                {
                    MaAlbum = Convert.ToInt16(Request["MaAlbum"]);
                    Updateluotnghe();
                    LoadAlbum();
                    LoadChiTietAlbum();
                    LoadAlbumCungTheLoai();
                    LoadBinhLuan(); 
                    txtComment.Text = "";
                }  
                if(Request["MaBXH"] != null)
                {   
                    MaBXH = Convert.ToInt16(Request["MaBXH"]);
                    loadBXH();

                }
            }
        }

        private void LoadBinhLuan()
        {
            reComment.DataSource = blAlbum.LayListBinhLuan(MaAlbum);
            reComment.DataBind();
        }

        private void loadBXH()
        {
            rePlayList.DataSource = bxhbus.loadBXH(MaBXH);
            rePlayList.DataBind();
            detail.Visible=false;
            comment.Visible = false;
            albumcungtheloai.Visible = false;
        }

        protected void btnBinhLuan_Click(object sender, EventArgs e)
        {
            BinhLuanAlbumDTO objBinhLuan = new BinhLuanAlbumDTO();
            NguoiDungDTO objNguoidung = (NguoiDungDTO)Session["ThanhVien"];
            objBinhLuan.MaAlbum = MaAlbum;
            objBinhLuan.TaiKhoan = objNguoidung.TaiKhoan;
            objBinhLuan.LoiBinh = txtComment.Text;
            if (blAlbum.ThemBinhLuan(objBinhLuan))
            {
                LoadBinhLuan();
            }
        }


        private void LoadAlbum()
        {
            rePlayList.DataSource = baihatBus.LayDanhSachBaiHatTheoAlbum(MaAlbum);
            rePlayList.DataBind();
        }

        private void Updateluotnghe()
        {
            abbus.UpdateLuotNgheAlbum(MaAlbum);
        }

        private void LoadChiTietAlbum()
        {
            DataTable dt = abbus.LayChiTietAlbum(MaAlbum);
            lbTenAlbum.Text = dt.Rows[0]["TenAlbum"].ToString();
            lbLuotNghe.Text = dt.Rows[0]["LuotNghe"].ToString();            
            lbNgayPhatHanh.Text = dt.Rows[0]["NgayPhatHanh"].ToString();
            lbTheLoai.Text = dt.Rows[0]["TenTheLoai"].ToString();
            imgHinhAnhAlbum.ImageUrl = "../../HinhAnh/Album/" + dt.Rows[0]["HinhAnh"].ToString();

            DataTable dt2 = abbus.LaySoBaiHatTrongAlbum(MaAlbum);
            lbSoBai.Text = dt2.Rows[0][0].ToString();

            DataTable dt3 = csbus.LayChiTietCasiTheoAlbum(MaAlbum);
            lbTenCS.Text = dt3.Rows[0]["TenCaSi"].ToString();
            lbNgaySinh.Text = dt3.Rows[0]["NgaySinh"].ToString();
            lbCongTy.Text = dt3.Rows[0]["CongTyDaiDien"].ToString();
            lbQuocGia.Text = dt3.Rows[0]["TenQuocGia"].ToString();
            imgHinhAnhCaSi.ImageUrl = "../../HinhAnh/CaSi/" + dt3.Rows[0]["HinhAnh"].ToString();
            MaTheLoai = int.Parse(dt.Rows[0]["MaTheLoai"].ToString());

        }


        private void LoadAlbumCungTheLoai()
        {
            reAlbumCungTheLoai.DataSource = abbus.LayDanhSachAlbumTheoTheLoai(MaTheLoai);
            reAlbumCungTheLoai.DataBind();

        }
    }
}